package locations.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import locations.dao.DAOFactory;
import locations.dao.InstitutionDAO;
import locations.model.Institution;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import locations.dao.InstitutionResponsibleDAO;
import locations.dao.InstitutionTypeDAO;


public class SaveInstitutionAction extends ActionSupport implements ModelDriven<Institution>, ServletRequestAware, ServletResponseAware {
	
	private static final long serialVersionUID = -8124967730231198191L;
	
	private HttpServletRequest request; 
	private HttpServletResponse response; 
	private Institution institution = new Institution();
	
	public String execute()
        {
            InstitutionResponsibleDAO daoIR = DAOFactory.getInstitutionResponsibleDAO();

            InstitutionTypeDAO daoIT = DAOFactory.getInstitutionTypeDAO();

            if (institution.getResponsible().getId() != null && institution.getResponsible().getApellido() == null)
            {
                this.institution.setResponsible(daoIR.retrieveById(institution.getResponsible().getId()));
            }
            if (institution.getType().getId() != null && institution.getType().getName() == null)
            {
                this.institution.setType(daoIT.retrieveById(institution.getType().getId()));
            }


            InstitutionDAO dao = DAOFactory.getInstitutionDAO();
            dao.save(this.institution);
            return SUCCESS;
	}
	
	
	
	@Override
	public Institution getModel() {
		return this.institution;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public Institution getInstitution() {
		return institution;
	}



	public void setInstitution(Institution institution) {
		this.institution = institution;
	}



	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	
	public HttpServletResponse getResponse(){
		return this.response;
	}
        
        public void validate()
        {
            boolean ok = true;
            if (this.getInstitution().getName().equals(""))
            {
                addFieldError("nombre", "El nombre no puede estar en blanco");
                    ok = false;
            }
            if (ok)
            {
                InstitutionDAO dao = DAOFactory.getInstitutionDAO();
                long auxId = (this.getInstitution().getId() == null? 0 : this.getInstitution().getId());
                if (dao.exists(this.getInstitution().getName(), auxId))
                {
                    addFieldError("nombre", "Ya existe una institución con ese nombre");
                    ok = false;
                }
            }
            if (this.getInstitution().getLocation().getLongitude() != null)
            {
                if (! this.getInstitution().getLocation().getLongitude().toString().matches("^[-]?[0-9]{1,3}\\.[0-9]+"))
                {
                        addFieldError("longitud", "La longitud ingresada no es valida");
                        ok = false;
                }
            }
            if (this.getInstitution().getLocation().getLatitude() != null)
            {
                if (! this.getInstitution().getLocation().getLatitude().toString().matches("^[-]?[0-9]{1,3}\\.[0-9]+"))
                {
                        addFieldError("latitud", "La latitud ingresada no es valida");
                        ok = false;
                }
            }
            if ((this.getInstitution().getLocation().getLatitude() == null) ^ (this.getInstitution().getLocation().getLongitude() == null))
            {
                addFieldError("longitud", "Es necesario que ingrese la latitud y la longitud, o ninguno de ellos");
                ok = false;
            }
        }
}
